package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import model.Stack;
import service.Service;
import dao.Dao;

public class MainFrame extends JFrame {
    private JPanel contentPane;
    private Controller controller = new Controller();
    private JButton btnAddStack, btnManageData, btnPickNextStack,
	    btnStatistics;
    private NotStockPanel pnlNotStock;
    private StockPanel pnlStock;
    private FinishedPanel pnlFinished;
    private WastesPanel pnlWastes;
    private AllStacksPanel pnlAllStacks;
    private JTabbedPane tpnlMain;
    private JMenuItem menuItem1 = new JMenuItem("About");
    private JMenuItem menuItem2 = new JMenuItem("Open manager");
    private JMenuItem menuItem3 = new JMenuItem("Create product type");
    private JMenuItem menuItem4 = new JMenuItem("Create process");
    private JMenuItem menuItem5 = new JMenuItem("Add stack");
    private JMenuItem menuItem6 = new JMenuItem("Statistics 1");
    private JMenuItem menuItem7 = new JMenuItem("Statistics 2");
    private JMenuItem menuItem8 = new JMenuItem("Statistics 3");

    /**
     * Create the frame.
     */
    public MainFrame() {
	setTitle("Carletti - Production management system");
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setBounds(100, 100, 830, 620);
	contentPane = new JPanel();
	contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
	setContentPane(contentPane);
	contentPane.setLayout(new BorderLayout(5, 0));

	JPanel pnlButtons = new JPanel();

	contentPane.add(pnlButtons, BorderLayout.WEST);
	pnlButtons.setLayout(new GridLayout(15, 0, 10, 5));

	tpnlMain = new JTabbedPane(JTabbedPane.TOP);

	contentPane.add(tpnlMain, BorderLayout.CENTER);

	Dao dao = Dao.getInstance();

	pnlStock = new StockPanel(dao.getStackWidth(), dao.getStackHeight());

	pnlNotStock = new NotStockPanel();
	pnlFinished = new FinishedPanel();
	pnlWastes = new WastesPanel();
	pnlAllStacks = new AllStacksPanel();

	tpnlMain.addTab("In Stock", pnlStock);
	tpnlMain.addTab("Not In Stock", pnlNotStock);
	tpnlMain.addTab("Finished", pnlFinished);
	tpnlMain.addTab("Wastes", pnlWastes);
	tpnlMain.addTab("All", pnlAllStacks);

	btnAddStack = new JButton("Add stack");
	btnPickNextStack = new JButton("Pick next stack");
	btnManageData = new JButton("Manage data");
	btnStatistics = new JButton("Statistics");

	pnlButtons.add(btnAddStack);
	pnlButtons.add(btnPickNextStack);
	pnlButtons.add(btnManageData);
	pnlButtons.add(btnStatistics);

	btnAddStack.addActionListener(controller);
	btnManageData.addActionListener(controller);
	btnPickNextStack.addActionListener(controller);
	btnStatistics.addActionListener(controller);

	JMenuBar menuBar;
	JMenu menuHelp, menuManage, menuStack, menuStatictics;

	menuBar = new JMenuBar();

	menuHelp = new JMenu("Help");
	menuManage = new JMenu("Manage data");
	menuStack = new JMenu("Stack operations");
	menuStatictics = new JMenu("Statistics");

	menuItem1.addActionListener(controller);
	menuItem2.addActionListener(controller);
	menuItem3.addActionListener(controller);
	menuItem4.addActionListener(controller);
	menuItem5.addActionListener(controller);
	menuItem6.addActionListener(controller);
	menuItem7.addActionListener(controller);
	menuItem8.addActionListener(controller);

	menuStack.add(menuItem5);

	menuManage.add(menuItem2);
	menuManage.add(menuItem3);
	menuManage.add(menuItem4);

	menuStatictics.add(menuItem6);
	menuStatictics.add(menuItem7);
	menuStatictics.add(menuItem8);

	menuHelp.add(menuItem1);

	menuBar.add(menuStack);
	menuBar.add(menuManage);
	menuBar.add(menuStatictics);
	menuBar.add(menuHelp);

	setJMenuBar(menuBar);

    }

    public StockPanel getStockPanel() {
	return pnlStock;
    }

    public void updateView() {
	for (int i = 0; i < tpnlMain.getTabCount(); i++) {
	    Component c = tpnlMain.getComponentAt(i);
	    ((Updatable) c).updateView();
	}
    }

    private class Controller implements ActionListener {
	@Override
	public void actionPerformed(ActionEvent e) {
	    if (e.getSource() == btnAddStack) {
		AddStack fr = new AddStack(MainFrame.this);
	    } else if (e.getSource() == btnManageData) {
		ManageData fr = new ManageData(MainFrame.this);

	    } else if (e.getSource() == btnPickNextStack) {
		Stack s = Service.getInstance().pickNextStack();
		if (s != null) {
		    StackInfoDialog info = new StackInfoDialog(s,
			    MainFrame.this);
		    info.setVisible(true);
		} else {
		    NoStackFoundDialog dlgNoStackFound = new NoStackFoundDialog(
			    MainFrame.this);
		    dlgNoStackFound.setVisible(true);
		}
	    } else if (e.getSource() == btnStatistics) {
		//
		System.out.println("Coming soon");
	    } else if (e.getSource() == menuItem1) {
		// About

	    } else if (e.getSource() == menuItem2) {
		// Open manager
		ManageData fr = new ManageData(MainFrame.this);
	    } else if (e.getSource() == menuItem3) {
		// Create product type
		AddEditType aet = new AddEditType(MainFrame.this);
	    } else if (e.getSource() == menuItem4) {
		// Create process
		AddEditProcess aep = new AddEditProcess(MainFrame.this);
	    } else if (e.getSource() == menuItem5) {
		// Add stack
		AddStack fr = new AddStack(MainFrame.this);
	    } else if (e.getSource() == menuItem6) {
		// Statistics 1
		System.out.println("Coming soon");
	    } else if (e.getSource() == menuItem7) {
		// Statistics 2
		System.out.println("Coming soon");
	    } else if (e.getSource() == menuItem8) {
		// Statistics 3
		System.out.println("Coming soon");
	    }

	}
    }

}
